//
//  YURefreshControl.swift
//  weibo117
//
//  Created by 宋禹奇 on 15/7/9.
//  Copyright © 2015年 宋禹奇. All rights reserved.
//

import UIKit
import FFAutoLayout

class YURefreshControl: UIRefreshControl {

    override init() {
        super.init()
    
        //添加刷新视图
        addSubview(refreshView)
        //刷新视图布局
        refreshView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: CGSize(width: 150, height: 60))
       
        //添加KVO监听方法
        self.addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
        
        
        
        
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit{
        //注销KVO监听
        removeObserver(self, forKeyPath: "frame")
    
    }
    
    //显示图标标记
    private var showTipFlag = false
    //正在加载标记
    private var loadingFlag = false
    
    //监听方法
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [NSObject : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        //print(frame)
        
        if frame.origin.y > 50 {
            return
        }
        
        if refreshing && !loadingFlag {
            refreshView.startLoadingAnim()
            
            loadingFlag = true
            return
       }
        
        if frame.origin.y < -50 && !showTipFlag{
        
            showTipFlag = true
            print("翻过来")
            refreshView.rotateTipIcon(showTipFlag)
            
        
        }else if frame.origin.y >= -50 && showTipFlag{
            showTipFlag = false
            print("翻过去")
            refreshView.rotateTipIcon(showTipFlag)
        }
        
    }
    
    override func endRefreshing() {
        super.endRefreshing()
        //关闭动画
        refreshView.stopLoadingAnim()
        //重新刷新标记
        loadingFlag = false
    }
    
    
    // 懒加载控件
    lazy var refreshView = YURefreshView.refreshView()

    
}

//刷新视图
class YURefreshView: UIView {

    @IBOutlet weak var tipView: UIView!
    @IBOutlet weak var loadingIcon: UIImageView!
    @IBOutlet weak var tipIcon: UIImageView!
    
    //从bundle中加载xib
    class func refreshView() -> YURefreshView {
        return NSBundle.mainBundle().loadNibNamed("YURefreshView", owner: nil, options: nil).last as! YURefreshView
    }

    //旋转提示图片
    private func rotateTipIcon(clockWise: Bool) {
        
        var angle = M_PI
        angle += clockWise ? -0.01 : 0.01
    
        UIView.animateWithDuration(0.5) { () -> Void in
            self.tipIcon.transform = CGAffineTransformRotate(self.tipIcon.transform, CGFloat(angle))
        }
    }
    //开启加载动画
    private func startLoadingAnim() {
        tipView.hidden = true
       
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.duration = 0.5
        loadingIcon.layer.addAnimation(anim, forKey: nil)
    }
    
    //停止加载动画
    private func stopLoadingAnim() {
        
        loadingIcon.layer.removeAllAnimations()
        
        tipView.hidden = false
        
    }
    

    
}